﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Data;
using System.Linq;
using System.Reflection;

namespace Dapper.Core
{
    public static class ServiceCollectionExtension
    {
        /// <summary>
        /// 添加 Dapper 拓展
        /// </summary>
        /// <param name="services"></param>
        /// <param name="option">数据库配置节点</param>
        /// <returns></returns>
        public static IServiceCollection AddDapper(this IServiceCollection services, Action<DBConfig> option)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }

            //注册数据库配置节点
            services.Configure<DBConfig>(option);

            //注册工作单元
            services.AddScoped<IUnitOfWork, UnitOfWork>();

            //注册普通基础仓储
            services.AddScoped<IBaseRepository, BaseRepository>();

            //注册泛型基础仓储
            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

            return services;
        }
    }
}
